/*
 * 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package dio.com.dao.conexao;

import dio.com.model.Facade;
import java.sql.*;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;

/**
 *
 * @author Diogenes
 */
public class AcessoCM {

    Connection con;
    String nome = "root";
    //Senha contabli
    //String password = "anakin2010";
    String password = "123456";
    PreparedStatement pstm;
    Statement statement;
    ResultSet rs;

    public Connection conectar() {
        try {
            Class.forName("com.mysql.jdbc.Driver");
            //Contabli
            //con = DriverManager.getConnection("jdbc:mysql://192.168.0.243:3306/controledeatividades?user=" + nome + "&password=" + password);
            con = DriverManager.getConnection("jdbc:mysql://localhost/controledeatividades?user=" + nome + "&password=" + password);
            con.setAutoCommit(false);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
            JOptionPane.showMessageDialog(null, e.getMessage(), "Erro! Driver não encontrado", JOptionPane.ERROR_MESSAGE);
        } catch (SQLException e) {
            e.printStackTrace();
            JOptionPane.showMessageDialog(null, e.getMessage(), "Erro!", JOptionPane.ERROR_MESSAGE);
        }
        return con;
    }

    public void desconectar() {
        try {
            con.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    public void rollBack() {
        try {
            con.rollback();
            desconectar();
        } catch (SQLException ex) {
            Logger.getLogger(Facade.class.getName()).log(Level.SEVERE, null, ex);
        }        
    }
    public void rollBack(Connection con) {
        try {
            con.rollback();
            desconectar();
        } catch (SQLException ex) {
            Logger.getLogger(Facade.class.getName()).log(Level.SEVERE, null, ex);
        }        
    }

    public void commit(Connection con) {
        try {

            con.commit();
            desconectar();

        } catch (Exception e) {
            Logger.getLogger(Facade.class.getName()).log(Level.SEVERE, null, e);
        }
    }

    public void commit() {
        try {
            con.commit();
            desconectar();
        } catch (Exception e) {
            Logger.getLogger(Facade.class.getName()).log(Level.SEVERE, null, e);
        }
    }
}
